package com.aiyige.page.publish.publishresult;

import android.app.Activity;
import android.content.Intent;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;

import com.aiyige.page.my.merchandisemanagement.model.TabItem;

public class PublishResult implements Parcelable {
    public static final String EXTRA_KEY_PUBLISH_RESULT = "com.aiyige.extra.EXTRA_KEY_PUBLISH_RESULT";
    public static final int DIALOG_TYPE_SUBMIT_REVIEW = 1;
    public static final int DIALOG_TYPE_SAVE_ONLY = 2;
    public static final int TARGET_TAB_INVALID = -1;
    public static final int TARGET_TAB_LEARN_VIDEO = 1;
    public static final int TARGET_TAB_MAJOR_COURSE = 2;
    public static final int TARGET_TAB_TRAING_CLASS = 3;
    public static final int TARGET_TAB_TRAING_CARD = 4;
    int dialogType;
    int targetTab;

    public PublishResult() {
    }

    private PublishResult(Builder builder) {
        setDialogType(builder.dialogType);
        setTargetTab(builder.targetTab);
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public int getDialogType() {
        return dialogType;
    }

    public void setDialogType(int dialogType) {
        this.dialogType = dialogType;
    }

    public int getTargetTab() {
        return targetTab;
    }

    public void setTargetTab(int targetTab) {
        this.targetTab = targetTab;
    }

    public static PublishResult obtainPublishResult(Intent intent) {
        if (intent == null) return null;
        return intent.getParcelableExtra(EXTRA_KEY_PUBLISH_RESULT);
    }

    public static void handlePublishResult(AppCompatActivity activity, PublishResult publishResult) {
        if (publishResult == null) return;
        switch (publishResult.getDialogType()) {
            case DIALOG_TYPE_SAVE_ONLY: {
                Integer targetTab;
                switch (publishResult.targetTab) {
                    case TARGET_TAB_LEARN_VIDEO:
                        targetTab = TabItem.TAB_ID_LEARN_VIDEO;
                        break;
                    case TARGET_TAB_MAJOR_COURSE:
                        targetTab = TabItem.TAB_ID_MAJOR_COURSE;
                        break;
                    case TARGET_TAB_TRAING_CARD:
                        targetTab = TabItem.TAB_ID_TRAING_CARD;
                        break;
                    case TARGET_TAB_TRAING_CLASS:
                        targetTab = TabItem.TAB_ID_TRAING_CLASS;
                        break;
                    default:
                        targetTab = null;
                        break;
                }
                SaveOnlyDialog.newBuilder()
                        .with(activity)
                        .targetTab(targetTab)
                        .show();
                break;
            }
            case DIALOG_TYPE_SUBMIT_REVIEW: {
                Integer targetTab;
                switch (publishResult.targetTab) {
                    case TARGET_TAB_LEARN_VIDEO:
                        targetTab = TabItem.TAB_ID_LEARN_VIDEO;
                        break;
                    case TARGET_TAB_MAJOR_COURSE:
                        targetTab = TabItem.TAB_ID_MAJOR_COURSE;
                        break;
                    case TARGET_TAB_TRAING_CARD:
                        targetTab = TabItem.TAB_ID_TRAING_CARD;
                        break;
                    case TARGET_TAB_TRAING_CLASS:
                        targetTab = TabItem.TAB_ID_TRAING_CLASS;
                        break;
                    default:
                        targetTab = null;
                        break;
                }
                SubmitReviewDialog.newBuilder()
                        .with(activity)
                        .targetTab(targetTab)
                        .show();
                break;
            }
        }
    }


    public static final class Builder {
        private int dialogType;
        private int targetTab;

        private Builder() {
        }

        public Builder dialogType(int val) {
            dialogType = val;
            return this;
        }

        public Builder targetTab(int val) {
            targetTab = val;
            return this;
        }

        public PublishResult build() {
            return new PublishResult(this);
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.dialogType);
        dest.writeInt(this.targetTab);
    }

    protected PublishResult(Parcel in) {
        this.dialogType = in.readInt();
        this.targetTab = in.readInt();
    }

    public static final Creator<PublishResult> CREATOR = new Creator<PublishResult>() {
        @Override
        public PublishResult createFromParcel(Parcel source) {
            return new PublishResult(source);
        }

        @Override
        public PublishResult[] newArray(int size) {
            return new PublishResult[size];
        }
    };
}
